//
//  CourseStudyMenuCell.m
//  ShowYork
//
//  Created by hxh on 2020/9/2.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonStudyMenuView.h"

@interface LessonStudyMenuView ()
@property (nonatomic,strong)UILabel *titleLb;
@property (nonatomic,strong)UILabel *subTitleLb;
@property (nonatomic,strong)UIImageView *checkImgView;
@property (nonatomic,strong)UIButton *nextBtn;
@property (nonatomic,strong)UILabel *nextLb;
@property (nonatomic,strong)UIImageView *nextImgView;
@property (nonatomic,copy)NSArray *titles;
@property (nonatomic,strong)UIView *lineView;
@property (nonatomic,strong)UIButton *checkBtn;
@property (nonatomic,assign)BOOL status;
@property (nonatomic,strong)UIView *line;
@property (nonatomic,strong)UIButton *lastBtn;
@end

@implementation LessonStudyMenuView


- (instancetype)initWithTitles:(NSArray *)titles selectedStatus:(BOOL)status{
    if (self = [super init]) {
        self.titles = titles;
        self.status = status;
        [self setUI];
     }
     return self;
}


- (void)setUI{
    // 设置默认高度
    self.frame = CGRectMake(0, 0, kScreenW, 100);
    self.titleLb = [[UILabel alloc]init];
    self.titleLb.textColor = RGBA(35, 24, 21, 1);
    self.titleLb.font = [UIFont boldSystemFontOfSize:20];
    self.titleLb.text = @"你的学习计划";
    [self addSubview:self.titleLb];
    
    [self.titleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(16);
        make.left.equalTo(self).offset(16);
    }];
    
    self.subTitleLb = [[UILabel alloc]init];
    self.subTitleLb.text = self.status?@"已学完":@"待学习";
    self.subTitleLb.textColor = RGBA(153, 153, 153, 1);
    self.subTitleLb.font = font(12);
    [self addSubview:self.subTitleLb];

    [self.subTitleLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.titleLb);
        make.left.equalTo(self.titleLb.mas_right).offset(16);
    }];
    
    
    self.checkImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"course_check_down"]];
    self.checkImgView.highlightedImage = [UIImage imageNamed:@"course_check_top"];
    [self addSubview:self.checkImgView];
    
    [self.checkImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.subTitleLb);
        make.left.equalTo(self.subTitleLb.mas_right).offset(3);
    }];
        
    self.checkBtn = [[UIButton alloc]init];
    [self.checkBtn addTarget:self action:@selector(checkMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.checkBtn];

    [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.subTitleLb);
        make.right.equalTo(self.checkImgView);
        make.top.equalTo(self.subTitleLb).offset(-5);
        make.bottom.equalTo(self.subTitleLb);
    }];
    self.nextImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"next_right_tag"]];
    [self addSubview:self.nextImgView];
    
    [self.nextImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-16);
        make.centerY.equalTo(self.subTitleLb);
        make.size.mas_equalTo(CGSizeMake(5,10));
    }];
    
    self.nextLb = [[UILabel alloc]init];
    self.nextLb.text = @"去选课";
    self.nextLb.textColor = RGBA(153, 153, 153, 1);
    self.nextLb.font = font(12);
    [self addSubview:self.nextLb];
    [self.nextLb mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.nextImgView.mas_left).offset(-8);
        make.centerY.equalTo(self.subTitleLb);
    }];
    
    self.nextBtn = [[UIButton alloc]init];
    [self.nextBtn addTarget:self action:@selector(nextMethod) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:self.nextBtn];
    [self.nextBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-16);
        make.centerY.equalTo(self.titleLb);
        make.top.bottom.mas_equalTo(self.titleLb);
        make.left.equalTo(self.nextLb.mas_left).offset(-30);
    }];
    
    CGFloat width = kScreenW/self.titles.count;
    
    [self.titles enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UIButton *btn = [[UIButton alloc]init];
        btn.tag = idx;
        [btn setTitle:obj forState:UIControlStateNormal];
        [btn setTitleColor:RGBA(102, 102, 102, 1) forState:UIControlStateNormal];
        [btn setTitleColor:[RPAppManager themColor] forState:UIControlStateSelected];
        btn.titleLabel.font = font(14);
        [self addSubview:btn];
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(width);
            make.bottom.equalTo(self).offset(-5);
            make.left.equalTo(self).offset(width * idx);
        }];
        if (idx == 0) {
            self.lastBtn = btn;
        }
//
        [btn addTarget:self action:@selector(coverMethod:) forControlEvents:UIControlEventTouchUpInside];
    }];
    
    self.lineView = [[UIView alloc]init];
    self.lineView.backgroundColor = [RPAppManager themColor];
    self.lineView.layer.cornerRadius = 2;
    self.lineView.layer.masksToBounds = YES;
    [self addSubview:self.lineView];
    
    
    [self.lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset(width/2 - 15);
        make.bottom.mas_equalTo(self);
        make.size.mas_equalTo(CGSizeMake(30, 4));
    }];
    
    
    self.line = [[UIView alloc]init];
    self.line.backgroundColor = RGBA(230, 230, 230, 1);
    [self addSubview:self.line];
    self.line.hidden = YES;
    [self.line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(self);
        make.height.mas_equalTo(0.5);
    }];
}

- (void)showLine{
    self.line.hidden = NO;
}
- (void)hideLine{
    self.line.hidden = YES;
}

- (void)updateStatus:(BOOL)status{
    self.status = status;
    self.subTitleLb.text = self.status?@"已学完":@"待学习";
    
    self.checkImgView.highlighted = NO;
}
- (void)coverMethod:(UIButton *)btn{
    
   
    CGFloat width = kScreenW/self.titles.count;
    
    [self.lineView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self).offset((width/2 - 15) + width * btn.tag);
    }];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuDidSelectAtIndex:)]) {
        [self.delegate menuDidSelectAtIndex:btn.tag];
    }
    self.lastBtn = btn;
}
- (void)nextMethod{
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuDidSelectClassAtIndex:)]) {
        [self.delegate menuDidSelectClassAtIndex:self.lastBtn.tag];
    }
}

- (void)checkMethod{
    
    
    self.checkImgView.highlighted = YES;
    
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    CGRect rect=[self.subTitleLb convertRect: self.subTitleLb.bounds toView:window];
    
    if (self.delegate && [self.delegate respondsToSelector:@selector(menuSwitchWithTouchPoint:status:)]) {
        [self.delegate menuSwitchWithTouchPoint:CGPointMake(rect.origin.x, CGRectGetMaxY(rect)) status:self.status];
    }
}

@end
